<template>
<div class="checkBox-container" :class="{'active': border_active}">
    <div class="checkBox" v-bind:class='{inline:flex}'>
        <i v-if='type === "radio" || type === "checklist" ' class="must-entry">*</i>
        <label class="radiolist-label" v-if='type === "radio" || type === "checklist" ' v-for="(item,index) in option" :key="index">
            <span class="mint-radio">
                <input v-if='type === "radio"' type="radio" class="radio-input" @change="postradio($event.currentTarget, item.value)" :checked="item.value===value" name='radio'>
                <input v-if='type === "checklist"' type="checkbox" class="radio-input" :value="item.value" @change="postCheckboxVal($event.currentTarget)"> 
                <span class="radio-core"></span>
            </span> 
            <span class="mint-radio-label">{{item.label}}</span>
        </label>
        <div class="switch-container" v-if="type === 'switch'">
            <span class="c-name"><slot></slot></span>
        <label class="mint-switch">
            <input type="checkbox" class="mint-switch-input" :checked="switchValue.select === value" @change="postSwitchVal($event.currentTarget)"> 
            <span class="mint-switch-core"></span> 
        </label> 
        </div>
    </div>
</div>
</template>

<script>
export default {
    data(){
        return{
            arr:[],
            switchVal:{},
            border_active: false
        }
    },
    props:{
        'value': {
            default: ''
        },
        'type':{
            default:'radio',
            validator:(InputType) =>{
                return ['radio', 'switch', 'checklist'].indexOf(InputType) !== -1
            }
        },
        'defValue':{
            default:() => {
                return ''
            }
        },
        'listValue':{
            default:() => {
                return []
            }
        },
        'option':{
            default:() =>{
                return [{
                label:'选项一',
                value:1
                },{
                label:'选项二',
                value:2
                },{
                label:'选项三',
                value:3
                }]
            }
        },
        'flex':{
            default:true
        },
        'checked':Boolean,
        'switchValue':{
            default:()=>{
               return {
                select: true,
                unselect: false
                } 
            }
        },

    // 0 不校验 1校验下是否为空 2校验下是否为数字
    'h-kind': {
      default: '0',
      validator: function (value) {
        // 这个值必须匹配下列字符串中的一个
        return ['0', '1', '2', '3'].indexOf(value) !== -1
      }
    },
    // 用来校验 当变化的时候 一般提交的时候变化
    'h-check': {
      default: 0
    },
    },
    watch: {
        hCheck () {
            console.log(1)
            if (this.hKind === '1' && this.value.toString() !== '') {
                this.border_active = false
            } else if (this.hKind === '1') {
                this.border_active = true
            }
        },
        value () {
            if (this.hKind === '1' && this.value.toString() !== '') {
                this.border_active = false
            } else if (this.hKind === '1') {
                this.border_active = true
            }
        }
    },
    methods:{
        // isChecked(v){
        //     let tF = false
        //     this.listValue.forEach(element => {
        //         if(element === v) 
        //         {
        //             tF = true
        //             // this.arr.push(v)
        //         }
        //     });
        //     console.log(tF)
        //     return tF
        // },
        postradio:function(target, value){
            if (target.checked) {
                this.$emit('radioVal',value)
            }
            //this.$emit('radioVal', e.value)
        },
        postCheckboxVal:function(e){
            if(e.checked === true){
                this.arr.push(e.value)
            }else{
                this.arr.splice(this.arr.indexOf(e.value),1)
            }
            console.log('arr',this.arr)
            this.$emit('checkboxVal', this.arr)
        },
        postSwitchVal:function(e){
            e.checked ? this.$emit('switchVal', this.switchValue.select) : this.$emit('switchVal', this.switchValue.unselect)
            //$emit('switchVal', $event.currentTarget.checked)
        }
    },
    created () {
    }
}
</script>
<style scoped>
.mint-radio-label{
    font-size: 0.75rem;
    color: #1a1e24;
    font-weight: bold;
}
label {
    margin: 0
}
.inline {
    display: flex;
    height: 1.5rem;
}
.mint-switch-input {
    display: none;
}
.mint-switch-input:checked + .mint-switch-core {
    border-color: #26a2ff;
    background-color: #26a2ff;
}
.mint-switch-core {
    display: inline-block;
    position: relative;
    width: 2.3rem;
    height: 1.2rem;
    border: 1px solid #d9d9d9;
    border-radius: 16px;
    box-sizing: border-box;
    background: #d9d9d9;
}
.mint-switch-core::before {
    width: 2.2rem;
    height: 1.1rem;
    background-color: #fdfdfd;
}
.mint-switch-core::after {
    width: 1.1rem;
    height: 1rem;
    background-color: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}
.mint-switch-core::after, .mint-switch-core::before {
    content: " ";
    top: 0;
    left: 0;
    position: absolute;
    transition: -webkit-transform .3s;
    transition: transform .3s;
    transition: transform .3s, -webkit-transform .3s;
    border-radius: 15px;
}
.radiolist-label {
    display: block;
    padding: 0 10px;
}
.radio-input {
    display: none;
    cursor: pointer;
}
.radio-input:checked + .radio-core {
    background-color: #26a2ff;
    border-color: #26a2ff;
}
.radio-core {
    box-sizing: border-box;
    display: inline-block;
    background-color: #fff;
    border-radius: 100%;
    border: 1px solid #ccc;
    position: relative;
    width: 20px;
    height: 20px;
    vertical-align: middle;
}
.radio-core::after {
    content: " ";
    border-radius: 100%;
    top: 5px;
    left: 5px;
    position: absolute;
    width: 8px;
    height: 8px;
    transition: -webkit-transform .2s;
    transition: transform .2s;
    transition: transform .2s, -webkit-transform .2s;
    transform: scale(0);
}
.radio-input:checked + .radio-core::after {
    background-color: #fff;
    transform: scale(1);
}
.must-entry {
    position: absolute;
    display: flex;
    align-items: center;
    height: 100%;
    top: 0;
    left: 0.15rem;
    color: #ff6b6b;
    font-style: normal;
    line-height: normal;
    font-size: 0.75rem;
    font-weight: 700
}
.checkBox-container{
    position: relative;
    padding: 1.25rem  0.45rem;
}
.checkBox-container::after{
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    margin: 0 auto;
    height: 0.05rem;
    background-color: #dddddd;
}
.checkBox-container.active::after{
    background-color: #ff0000;
}
.switch-container{
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    height: 1.5rem;
}
.c-name{
    color: #1a1e24;
    font-weight: bold;
    padding-right: 0.25rem;
    font-size: 0.75rem;
}
</style>
